<template>
  <div class="aui-wrapper aui-page__login">
    <div class="aui-content__wrapper">
      <main class="aui-content">
        <!-- <div class="login-header">
          <h2 class="login-brand">{{ $t('brand.lg') }}</h2>
        </div> -->
        <div class="login-body">
          <!-- <h3 class="login-title">{{ $t('login.title') }}</h3> -->
          <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" status-icon>
            <!-- <el-form-item>
              <el-select v-model="$i18n.locale" class="w-percent-100">
                <el-option v-for="(val, key) in i18nMessages" :key="key" :label="val._lang" :value="key"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item prop="username">
              <el-input v-model="dataForm.username" :placeholder="$t('请输入账号')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-user"></use></svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="dataForm.password" type="password" :placeholder="$t('请输入密码')">
                <span slot="prefix" class="el-input__icon">
                  <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-lock"></use></svg>
                </span>
              </el-input>
            </el-form-item>
            <el-form-item prop="captcha">
              <el-row :gutter="20">
                <el-col :span="11">
                  <el-input v-model="dataForm.captcha" :placeholder="$t('请输入验证码')">
                    <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-safetycertificate"></use></svg>
                    </span>
                  </el-input>
                </el-col>
                <el-col :span="8" class="login-captcha">
                  <img :src="captchaPath">
                </el-col>
                <el-col :span="5">
                  <span class="iconfont el-icon-refresh-right" @click="getCaptcha()"></span>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="dataFormSubmitHandle()" class="w-percent-100">{{ $t('login.title') }}</el-button>
            </el-form-item>
            <el-form-item prop="checked" class="checked">
              <el-checkbox v-model="checked">记住密码</el-checkbox>
            </el-form-item>
          </el-form>
        </div>
        <div class="login-footer">
          <p>
            <a href="https://demo.renren.io/security-enterprise" target="_blank">{{ $t('login.demo') }}</a>
          </p>
          <p><a href="https://www.renren.io/" target="_blank">{{ $t('login.copyright') }}</a>2019 © renren.io</p>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
import { messages } from '@/i18n'
import { getUUID } from '@/utils'
const Base64 = require('js-base64').Base64
export default {
  data () {
    return {
      i18nMessages: messages,
      captchaPath: '',
      dataForm: {
        username: '',
        password: '',
        uuid: '',
        captcha: ''
      },
      checked: false
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCaptcha()
    // 在页面加载时从cookie获取登录信息
    let username = Cookies.get('username')
    let password = Cookies.get('password')
    // 如果存在赋值给表单，并且将记住密码勾选
    if (username) {
      this.dataForm.username = Base64.decode(username)
      this.dataForm.password = Base64.decode(password)
      this.checked = true
    }
  },
  methods: {
    // 获取验证码
    getCaptcha () {
      this.dataForm.uuid = getUUID()
      this.captchaPath = `${window.SITE_CONFIG['apiURL']}/captcha?uuid=${this.dataForm.uuid}`
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/login', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            this.getCaptcha()
            return this.$message.error(res.msg)
          }
          Cookies.set('token', res.data.token)
          if (this.checked) {
            Cookies.set('username', Base64.encode(this.dataForm.username))
            Cookies.set('password', Base64.encode(this.dataForm.password))
          } else {
            Cookies.set('username', '')
            Cookies.set('password', '')
          }
          this.$router.replace({ name: 'home' })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
.aui-page__login::before{
  background-image: none;
}
.aui-page__login::after{
  background-color: transparent;
}
.aui-page__login .aui-content{
  background-image: url('../../assets/img/login_bg.jpg');
  background-size: cover;
}
.aui-page__login .login-body{
  position: absolute;
  left: calc(50% + 94px);
  top: 260px;
  .login-captcha > img{
    cursor: default;
  }
  .el-button{
    background-image: linear-gradient(to right, #6681F1 , #918DFC);
    border: none;
    border-radius: 20px;
  }
  .checked{
    /deep/.el-form-item__content{
      display: flex;
      justify-content: flex-end;
      .el-checkbox__label{
        color: #B3B3B3;
      }
    }
  }
  .iconfont{
    cursor: pointer;
    font-size: 28px;
    line-height: 40px;
    color: #9186E3;
  }
  /deep/.el-input__inner:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px #F0F0F7 inset;
    -webkit-text-fill-color: #333;
  }
  /deep/.el-input__inner{
    border: none;
    background-color: #F0F0F7;
  }
}
.aui-page__login .login-body, .aui-page__login .login-footer{
  width: 430px;
}

</style>
